package com.hugang.juc.assistclass;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 计数器，正着数；即人齐了才开会。
 *
 * @author hg
 * @date 2020/8/20 16:19
 */
public class CyclicBarrierDemo {

    public static void main(String[] args) throws Exception {
        //public CyclicBarrier(int parties, Runnable barrierAction)
        CyclicBarrier cyclicBarrier = new CyclicBarrier(6, () -> System.out.println("人齐了，开会"));
        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " ---> 到了");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();

        }
    }


}
